/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jfTelaLogin.java
 *
 * Created on 19/08/2009, 17:04:54
 */
package View;

import Control.UTI;
import Model.Base;
import java.io.File;
import javax.swing.ImageIcon;
import sislab.Main;

/**
 *
 *  @author Augusto & Gustavo
 */
public class jfTelaLogin extends javax.swing.JFrame {

    /**
     * Creates new form jfTelaLogin
     */
    public jfTelaLogin() {
        initComponents();
        /*System.out.println(getClass().getResource("/relatorios/relRetiradaDevolucao.jrxml").getAuthority());
        System.out.println("FILE"+getClass().getResource("/relatorios/relRetiradaDevolucao.jrxml").getFile());
        System.out.println(getClass().getResource("/relatorios/relRetiradaDevolucao.jrxml").getHost());
        System.out.println("PATH"+getClass().getResource("/relatorios/relRetiradaDevolucao.jrxml").getPath());
        System.out.println(getClass().getResource("/relatorios/relRetiradaDevolucao.jrxml").getRef());*/
        
       try {
            server = UTI.abrirArquivo(new File("banco"));
        } catch (Exception e) {

            server = "jdbc:mysql://localhost:3306/sislab";
            e.printStackTrace();
        }
       //server = "jdbc:mysql://localhost:3306/conem";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpLogo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jlLogin = new javax.swing.JLabel();
        jlSenha = new javax.swing.JLabel();
        jtfLogin = new javax.swing.JTextField();
        jpfSenha = new javax.swing.JPasswordField();
        jPanel3 = new javax.swing.JPanel();
        jbSair = new javax.swing.JButton();
        jbLimpar = new javax.swing.JButton();
        jbEntrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("CONEM");
        setBackground(new java.awt.Color(102, 102, 102));
        setResizable(false);

        jpLogo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jpLogo.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Arial Black", 1, 100)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SisLab");
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });
        jpLogo.add(jLabel1, java.awt.BorderLayout.CENTER);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel2MouseClicked(evt);
            }
        });

        jlLogin.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jlLogin.setText("Login:");

        jlSenha.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jlSenha.setText("Senha:");

        jtfLogin.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jtfLogin.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtfLoginFocusGained(evt);
            }
        });

        jpfSenha.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jpfSenha.setEchoChar('*');
        jpfSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jpfSenhaActionPerformed(evt);
            }
        });
        jpfSenha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jpfSenhaFocusGained(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlSenha)
                    .addComponent(jlLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpfSenha, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .addComponent(jtfLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlLogin)
                    .addComponent(jtfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlSenha)
                    .addComponent(jpfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jbSair.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jbSair.setText("Sair");
        jbSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSairActionPerformed(evt);
            }
        });

        jbLimpar.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jbLimpar.setText("Limpar");
        jbLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLimparActionPerformed(evt);
            }
        });

        jbEntrar.setFont(new java.awt.Font("Lucida Console", 1, 18)); // NOI18N
        jbEntrar.setText("Entrar");
        jbEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEntrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbSair, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbLimpar, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbEntrar, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jbSair, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbEntrar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jpLogo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpLogo, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSairActionPerformed
        super.dispose();
}//GEN-LAST:event_jbSairActionPerformed

    private void jbLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLimparActionPerformed
        jtfLogin.setText("");
        jpfSenha.setText("");
        jtfLogin.requestFocus();
}//GEN-LAST:event_jbLimparActionPerformed

    private void jbEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbEntrarActionPerformed
        autenticar();
}//GEN-LAST:event_jbEntrarActionPerformed

    private void jpfSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jpfSenhaActionPerformed
        autenticar();
    }//GEN-LAST:event_jpfSenhaActionPerformed

    private void jtfLoginFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtfLoginFocusGained
        jtfLogin.selectAll();
    }//GEN-LAST:event_jtfLoginFocusGained

    private void jpfSenhaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jpfSenhaFocusGained
        jpfSenha.selectAll();
    }//GEN-LAST:event_jpfSenhaFocusGained

    private void jPanel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel2MouseClicked
        if (evt.getClickCount() == 3) {
            server = UTI.lerString(this, "Informe o servidor:", server);
        }
    }//GEN-LAST:event_jPanel2MouseClicked

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        if (evt.getClickCount() == 3) {
            String senhaTela =  UTI.lerSenha(this, "Digite a senha");
           if (senhaTela.equals("dnovo171")){
            if (UTI.confirmar(this, "Instalar Banco de Dados", "AVISO")) {
                try {
                    server = UTI.lerString(this, "Informe o Servidor", server);
                    String usuario = UTI.lerString(this, "Informe o usuário:", "conem");
                    String senha = UTI.lerSenha(this, "Informe a senha:");
                    Base b = new Base(server, usuario, senha);
                    for (int i = 0; i < 100 && !b.isOk(); i++) {
                        try {
                            Thread.sleep(50);
                        } catch (Exception e) {
                        }
                    }

                    if (b.isOk()) {
                        UTI.aviso(this, "Instalação concluída");
                        UTI.gravarArquivo(new File("banco"), server);

                    } else {
                        UTI.erro(this, "Usuário ou senha incorretos.");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
           }
        }
    }//GEN-LAST:event_jLabel1MouseClicked
    // private String server = "jdbc:mysql://10.3.0.79:3306/conem";
    private String server;

    private void autenticar() {
        try {
            String login = jtfLogin.getText().trim();
            String pas = new String(jpfSenha.getPassword()).trim();
            //Base b = new Base("jdbc:mysql://localhost:3306/drentrega", login, pas);
            //Base b = new Base("jdbc:mysql://gutowolf.gotdns.com:3306/drentrega", login, pas);
            //System.out.println("|" + login + "|" + pas + "|");
            Base b = new Base(server, login, pas);
            for (int i = 0; i < 100 && !b.isOk(); i++) {
                try {
                    Thread.sleep(50);
                } catch (Exception e) {
                }
            }

            if (b.isOk()) {
                //if (true) {
                this.dispose();
                Main.tp = new jfTelaPrincipal();
                Main.tp.setVisible(true);
            } else {
                // UTI.erro(this, "Usuário ou senha incorretos.");
                jtfLogin.requestFocus();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JButton jbEntrar;
    private javax.swing.JButton jbLimpar;
    private javax.swing.JButton jbSair;
    private javax.swing.JLabel jlLogin;
    private javax.swing.JLabel jlSenha;
    private javax.swing.JPanel jpLogo;
    private javax.swing.JPasswordField jpfSenha;
    private javax.swing.JTextField jtfLogin;
    // End of variables declaration//GEN-END:variables
}
